home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcplusx.zip / MOUSE.CPP < prev    next >
C/C++ Source or Header  |  1991-02-28  |  8KB  |  330 lines

  1. //
  2. // mouse.cpp    - implementation for class Mouse
  3. // Author        - Robin W. McKean
  4. // Last Update    - February 23,1991
  5. // Copyright (C) 1991 All rights reserved
  6. //
  7. // This file remains the property of the author, Robin W. McKean.  You are
  8. // free to use and change it as you see fit.  This module, nor its object
  9. // code, may not however be included  in any packaged software without the
  10. // written consent of the author.
  11. //
  12.  
  13. // Contents ----------------------------------------------------------------
  14. //
  15. //        Mouse::Mouse
  16. //        Mouse::isA
  17. //        Mouse::nameOf
  18. //        Mouse::processA
  19. //        Mouse::pollDevice
  20. //
  21. // Description
  22. //
  23. //      Defines the class Mouse.  The purpose of this class is to allow
  24. //      the mouse to be used as input also, in a manner that is tranparent
  25. //        to the programmer.  This is utlimate integrated input devices!
  26. //            
  27. // End ---------------------------------------------------------------------
  28.  
  29. // Interface dependencies --------------------------------------------------
  30.  
  31. #ifndef _IOSTREAM_H
  32. #include <iostream.h>
  33. #endif
  34.  
  35. #ifndef _USETYPES_H
  36. #include <usetypes.h>
  37. #endif
  38.  
  39. #ifndef _GEN_H
  40. #include <gen.h>
  41. #endif
  42.  
  43. #ifndef _EVENT_H
  44. #include <event.h>
  45. #endif
  46.  
  47. #ifndef _MOUSE_H
  48. #include <mouse.h>
  49. #endif
  50.  
  51. // End Interface dependencies ----------------------------------------------
  52.  
  53. // Implementation dependencies ---------------------------------------------
  54.  
  55. #ifndef _DOS_H
  56. #define _DOS_H
  57. #include <dos.h>
  58. #endif
  59.  
  60. #ifndef _CONIO_H
  61. #define _CONIO_H
  62. #include <conio.h>
  63. #endif
  64.  
  65. // End Implementation dependencies -----------------------------------------
  66.  
  67. // Member Function //
  68.  
  69. Mouse::Mouse( int initStatus ) : Device( initStatus )
  70.  
  71. // Summary ------------------------------------------------------------------
  72. //
  73. //        Sets the initial status of the mouse.  It is the responsibility of
  74. //        of the constructor to determine if a mouse driver is present on the
  75. //        system currently in use.
  76. //
  77. // End ---------------------------------------------------------------------
  78. {
  79.     REGS regs;
  80.  
  81.     // Set the device type, and mouse button press information to defualt
  82.     type = D_MOUSE;
  83.     mouseButtonPressed = 0;
  84.  
  85.     // Use BIOS ( DOS ) to call mouse driver
  86.     regs.x.ax = 0;
  87.     int86( 0x33, ®s, ®s );
  88.  
  89.     // If device returned 0xffff, then device has been reset
  90.     if( regs.x.ax == -1 )
  91.     {
  92.         status = D_ON;
  93.         cellWidth = 8;                // Reset these to change mouse bounds
  94.         cellHeight = 8;
  95.         lines = 24;
  96.         columns = 79;
  97.     }
  98.     else
  99.         status = D_INACTIVE;        // No device available for us!
  100. }
  101.  
  102. // End Mouse::Mouse //
  103.  
  104. // Member Function //
  105.  
  106. #pragma argsused
  107. Mouse::Mouse( Device& theDevice )
  108.  
  109. // Description -------------------------------------------------------------
  110. //
  111. //        Copies one Mouse into another
  112. //
  113. // Parameters
  114. //
  115. //        Device&
  116. //
  117. //        The Mouse to be copied
  118. //
  119. // End ---------------------------------------------------------------------
  120. {
  121.     mouseButtonPressed = ( ( Mouse& ) theDevice ).mouseButtonPressed;
  122.     status = ( ( Mouse& ) theDevice ).status;
  123. }
  124.  
  125. // End Mouse::Mouse //
  126.  
  127. // Member Function
  128.  
  129. Mouse::~Mouse( )
  130.  
  131. // Description --------------------------------------------------------------
  132. //
  133. //        This destructor turns the mouse cursor off.  Evidently there is some
  134. //        bug about the program supposedly automatically turning the mouse off.
  135. //        It never hurts to be sure.
  136. //
  137. // End ----------------------------------------------------------------------
  138. {
  139.     REGS regs;
  140.  
  141.     // Turn the mouse cursor off
  142.     regs.x.ax = 2;
  143.     int86( 0x33, ®s, ®s );
  144. }
  145. // Mouse::~Mouse //
  146.  
  147. // Member Function //
  148.  
  149. classType Mouse::isA( ) const
  150.  
  151. // Description -------------------------------------------------------------
  152. //
  153. //        Returns a value representation of a class
  154. //
  155. // End ---------------------------------------------------------------------
  156. {
  157.     return mouseClass;
  158. }
  159.  
  160. // End Mouse::isA( ) //
  161.  
  162. // Member Function //
  163.  
  164. char *Mouse::nameOf( ) const
  165.  
  166. // Description -------------------------------------------------------------
  167. //
  168. //        Returns a character representation of a class
  169. //
  170. // End ---------------------------------------------------------------------
  171. {
  172.     return "Mouse";
  173. }
  174.  
  175. // End Mouse::nameOf //
  176.  
  177. // Member Function //
  178.  
  179. void Mouse::pollDevice( )
  180.  
  181. // Description -------------------------------------------------------------
  182. //
  183. //        This reads the mouse for input.  If a mouse button is pressed, it
  184. //        it checks to see if the mouse button was pressed last time we came
  185. //        through.  If it was, we have a continued selection.  Otherwise we
  186. //        have a first time selection.  If a button is not pressed, nothing
  187. //        happens.
  188. //
  189. // End ---------------------------------------------------------------------
  190.  
  191. {
  192.     REGS regs;
  193.     Event event;
  194.  
  195.     event.type = 0;
  196.  
  197.     // if mouse not properly installed, return
  198.     if( status == D_INACTIVE || status == D_OFF ) return;
  199.  
  200.     // Get the current mouse status
  201.     regs.x.ax = 3;
  202.     regs.x.bx = 0;
  203.     int86( 0x33, ®s, ®s );
  204.  
  205.     event.point.row = regs.x.dx / cellHeight;
  206.     event.point.column = regs.x.cx / cellWidth;
  207.  
  208.     // if the last mouse operation was not a button press, get one now
  209.     if( !mouseButtonPressed )
  210.     {
  211.         mouseButtonPressed = regs.x.bx;
  212.         if( mouseButtonPressed )
  213.         {
  214.             event.type = E_MOUSE;
  215.             switch( mouseButtonPressed )
  216.             {
  217.                 case 1:
  218.                     event.typeCode = M_LEFT_SELECT;     // Left button press
  219.                     break;
  220.                 case 2:
  221.                     event.typeCode = M_RIGHT_SELECT;        // Right button press
  222.                     break;
  223.                 case 4:
  224.                     event.typeCode = M_MIDDLE_SELECT;     // Middle button press
  225.                     break;
  226.             }
  227.         }
  228.     }
  229.  
  230.     else
  231.     {
  232.         // Last event from here was a button press
  233.         event.type = E_MOUSE;
  234.  
  235.         // Check to see who has been released
  236.         if( regs.x.bx == mouseButtonPressed )
  237.         {
  238.             // Button still is being pressed, so send continue
  239.             switch( regs.x.bx )
  240.             {
  241.                 case 1:
  242.                     event.typeCode = M_LCONTINUE_SELECT;
  243.                     break;
  244.                 case 2:
  245.                     event.typeCode = M_RCONTINUE_SELECT;
  246.                     break;
  247.                 case 4:
  248.                     event.typeCode = M_MCONTINUE_SELECT;
  249.                     break;
  250.             }
  251.         }
  252.         else
  253.         {
  254.             // Button has to be released, send proper release button
  255.             event.type = E_MOUSE;
  256.             switch( mouseButtonPressed )
  257.             {
  258.                 case 4:
  259.                     event.typeCode = M_MIDDLE_RELEASE;
  260.                     break;
  261.                 case 2:
  262.                     event.typeCode = M_RIGHT_RELEASE;
  263.                     break;
  264.                 default:
  265.                     event.typeCode = M_LEFT_RELEASE;
  266.             }
  267.             mouseButtonPressed = 0;
  268.         }
  269.         
  270.     }   
  271.  
  272.     // If we found a event, stuff the queue with it
  273.     if( event.type && theEventManager ) theEventManager->putEvent( event, FALSE );
  274.  
  275. }
  276.  
  277. int Mouse::processA( Event& theEvent )
  278. {
  279.     REGS regs;
  280.  
  281.     // If we are not active, then don't process event
  282.     if( status == D_INACTIVE || status == D_OFF ) return( 0 );
  283.  
  284.     switch( theEvent.typeCode )
  285.     {
  286.         case D_ON:
  287.         case D_SHOW:
  288.             regs.x.ax = 1;                        // Turn mouse on
  289.             int86( 0x33, ®s, ®s );
  290.             break;
  291.         case D_OFF:                             // Turn mouse off
  292.         case D_HIDE:
  293.             regs.x.ax = 2;
  294.             int86( 0x33, ®s, ®s );
  295.             break;
  296.         case D_POSITION:                        // position mouse
  297.             regs.x.ax = 4;
  298.             regs.x.cx = ( unsigned int )theEvent.point.column * cellWidth;
  299.             regs.x.dx = ( unsigned int )theEvent.point.row * cellHeight;
  300.             int86( 0x33, ®s, ®s );
  301.             break;
  302.         case D_INIT:
  303.             // Set the mouses horizontal bounds
  304.             regs.x.ax = 7;
  305.             regs.x.cx = 0;
  306.             regs.x.dx = ( unsigned int )columns * cellWidth;
  307.             int86( 0x33, ®s, ®s );
  308.  
  309.             // Set the mousees vertical bounds
  310.             regs.x.ax = 8;
  311.             regs.x.cx = 0;
  312.             regs.x.dx = ( unsigned int )lines * cellHeight;
  313.             int86( 0x33, ®s, ®s );
  314.  
  315.             // Set the cursor state
  316.             regs.x.ax = 10;   
  317.             regs.x.bx = 0;   
  318.             regs.x.cx = 0xffff;
  319.             regs.x.dx = 0x7700;
  320.             int86(0x33,®s,®s);
  321.  
  322.             // Now turn the mouse cursor on
  323.             regs.x.ax = 1;
  324.             int86( 0x33, ®s, ®s );
  325.             break;
  326.     }
  327.  
  328.     return( 0 );
  329. }
  330.